package com.crm.controller.saling;

import com.crm.pojo.Pay;
import com.crm.pojo.PlaningPay;
import com.crm.service.saling.PayService;
import com.crm.service.saling.PlaningPayService;
import com.crm.vo.PayClientContractUserInfoVOList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dr5hx on 2018/9/3
 */
@Controller
public class PayController {
    @Autowired
    private PayService payService;
    @Autowired
    private PlaningPayService planingPayService;

    @GetMapping("toPayList")
    public String toPayList() {
        return "saling/payList";
    }

    @PostMapping("getPayList")
    @ResponseBody
    public Map getPayList(int page, int rows) {

        try {
            List<PayClientContractUserInfoVOList> list = payService.getPayClientContractUserInfoVOList(page, rows);
            Long count = payService.getPayListCount();
            Map map = new HashMap();
            map.put("rows", list);
            map.put("total", count);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping("withdrawPayRecord")
    @ResponseBody
    public Boolean withdrawPayRecord(Long payId) {
        Pay pay = payService.findOne(payId);
        if (pay != null) {
            PlaningPay planingPay = planingPayService.findOne(pay.getFkPayPlaningPayId().longValue());
            if (planingPay != null) {
                planingPay.setPlaningPayState("未完成");
                pay.setPayIsDel("是");
                return planingPayService.save(planingPay) != null && payService.save(pay) != null;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}
